package com.baseDemo.web.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.baseDemo.web.entity.AuthorityEntity;
import com.baseDemo.web.entity.RoleEntity;
import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.service.role.RoleService;
import com.share.hibernate.baseDaoSupport.PageUtil;
import com.share.hibernate.baseDaoSupport.TreeUtil;

@Controller
@RequestMapping(value = "/role", produces = "application/json;charset=UTF-8")
public class RoleController {

	@Resource
	RoleService roleService;

	@RequestMapping(value="/getAllRole")
	@ResponseBody
	public PageUtil getAllRole(@RequestParam("page") Integer page,
			@RequestParam("limit") Integer limit,
			@RequestParam("start") Integer start) {
		return  roleService.getListForPageToJson(start, limit);
	}
	@RequestMapping(value = "/addRole")
	@ResponseBody
	public String addRole(@RequestBody RoleEntity roleEntity){
		return "{'id':'" + roleService.save(roleEntity) + "'}";
	}
	
	@RequestMapping(value = "/destroyRole")
	@ResponseBody
	public String destroyRole(@RequestBody RoleEntity roleEntity) {
		return roleService.delete(roleEntity);
	}
	
	@RequestMapping(value = "/updateUser")
	@ResponseBody
	public String updateRole(@RequestBody RoleEntity roleEntity) {
		return roleService.update(roleEntity);
	}
	@RequestMapping(value = "/getUserByRole")
	@ResponseBody
	public Set<UserEntity> getUserByRole(@RequestParam String roleId){
		return roleService.getUsers(roleId);
	}
	@RequestMapping(value = "/addUserForRole")
	@ResponseBody
	public String addUserForRole(@RequestBody Set<UserEntity> userEntities,@RequestParam String roleId){
		return roleService.addUsersForRole(roleId, userEntities);
	}
	@RequestMapping(value = "/deleteUserForRole")
	@ResponseBody
	public String deleteUserForRole(@RequestBody Set<UserEntity> userEntities,@RequestParam String roleId){
		return roleService.deleteUsersForRole(roleId, userEntities);
	}
	@RequestMapping(value = "/getAuthority")
	@ResponseBody
	public List<TreeUtil> getAuthority(HttpServletRequest request,HttpServletResponse response){
		return roleService.getAuthorityList();
	}
	@RequestMapping(value = "/updateAuthority")
	@ResponseBody
	public String updateAuthority(@RequestBody List<String> authorities,@RequestParam String roleId){
		return roleService.updateAuthority(authorities, roleId);
	}
	@RequestMapping(value = "/getAuthoritiesByRole")
	@ResponseBody
	public Set<AuthorityEntity> getAuthoritiesByRole(@RequestParam String roleId){
		return roleService.getAuthoritiesByRole(roleId);
	}
}
